package com.ms.service.module.fish.message;

import java.nio.ByteBuffer;

import org.jboss.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ms.service.module.fish.test.ClientRobot;

public abstract class FishBaseMessage implements IWrite{

	private static Logger logger = LoggerFactory.getLogger(FishBaseMessage.class);
	
	protected Integer cmd;
	protected byte[] byteArray;

	@Override
	public void write(ByteBuffer buf) {
		if(cmd != null){
			if(logger.isInfoEnabled()) {
				logger.info("发包CmdId：" + cmd);
			}
			buf.putInt(cmd);
		}
		writeBody(buf);
	}
	
	@Override
	public byte[] getByteArray(){
		return byteArray;
	}
	
	@Override
	public void setByteArray(byte[] byteArray){
		this.byteArray = byteArray;
	}
	
	@Override
	public Integer getCmd() {
		return cmd;
	}
	
	@Override
	public void setCmd(int cmd){
		this.cmd = cmd;
	}

	public static void writeString(ByteBuffer buf,String str){
		byte[] bytes = str.getBytes(CharsetUtil.UTF_8);
		buf.putShort((short) bytes.length);
		buf.put(bytes);
	}
	
	
	public abstract void writeBody(ByteBuffer buf);
	
	

}
